//
//  WDCarTypeSheet.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2017/2/20.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDCarTypeSheet.h"
#import "WDCustomBtn.h"
#define fontSize 15
#define Duration 0.5
#define headH 40.5
#define btnH 85
#define btnW [UIScreen mainScreen].bounds.size.width / 2

@interface WDCarTypeSheet ()
@property (nonatomic,strong) UIView * contentView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation WDCarTypeSheet

static NSArray * allbus = nil;
static NSArray *selebtn = nil;

- (instancetype)initWithButtons:(NSArray *)allButtons seleBtn:(NSArray *)seleBtn
{
    allbus = allButtons;
    selebtn = seleBtn;
    WDCarTypeSheet * sheet = [[WDCarTypeSheet alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [sheet set];
    return sheet;
}

-(void)set
{
    [UIView animateWithDuration:Duration animations:^{
        _contentView.frame = CGRectMake(0, WDScreenH - btnH - headH - 64, WDScreenW, btnH + headH);
    }];
    
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame])
    {
        UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        back.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.3];
        [self addSubview:back];
        
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, WDScreenH,  WDScreenW,btnH + headH)];
        _contentView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_contentView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, [UIScreen mainScreen].bounds.size.width * 0.5, headH)];
        _titleLabel.textColor = ColorForRGB(0x4c4c4c);
        _titleLabel.font = [UIFont systemFontOfSize:fontSize];
        [_contentView addSubview:_titleLabel];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, headH - 1, [UIScreen mainScreen].bounds.size.width, 1)];
        line.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
        [_contentView addSubview:line];
        
        for (int i = 0; i<allbus.count; i++)
        {
            WDCustomBtn * bu = [WDCustomBtn buttonWithType:UIButtonTypeCustom];
            bu.tag = i;
            bu.backgroundColor = [UIColor whiteColor];
            bu.frame = CGRectMake(i * btnW, headH, btnW, btnH);
            [_contentView addSubview:bu];
            
            NSString *str = (i == 0 ? @"二手车":@"新车");
            
            [bu setTitle:str forState:UIControlStateNormal];
            [bu setTitleColor:ColorForRGB(0x4c4c4c) forState:UIControlStateNormal];
            bu.titleLabel.font = [UIFont systemFontOfSize:15];
            [bu setImage:[UIImage imageNamed:allbus[i]] forState:UIControlStateNormal];
            [bu setImage:[UIImage imageNamed:selebtn[i]] forState:UIControlStateHighlighted];
            [bu setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0.8 alpha:0.4]] forState:UIControlStateHighlighted];
            [bu addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return self;
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = title;
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self removeFromSuperview];
}
-(void)clickButton:(UIButton*)button
{
    [self.delegate  carTypeSheet:self clickButton:button.tag];
    
    [self removeFromSuperview];
}

@end
